package com.applink.sell_proof;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.view.Window;

import com.applink.sell_proof.broadcast_receiver.NetworkConnectivityReceiver;
import com.applink.sell_proof.util.Utility;
import com.google.android.gcm.GCMRegistrar;

public class NewSplashActivity extends Activity {
	private static final int LOAD_DATA_SYNC_ONLINE = 0;
	public static final int LOAD_DATA_SYNC_OFFLINE = 1;
	private Intent intent;
	public static NewSplashActivity getInstant;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.splash);
		getInstant = this;
		initView();
	}

	private void initView() {
		try {
			Utility.checkNetwork(this);
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (EMarketing.isOnline()) {
			mHandler.sendEmptyMessage(LOAD_DATA_SYNC_ONLINE);
		}
	}

	public Handler mHandler = new Handler(new Handler.Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case LOAD_DATA_SYNC_ONLINE:
				loadDataSync(true);
				break;

			case LOAD_DATA_SYNC_OFFLINE:
				loadDataSync(false);
				break;

			default:
				break;
			}
			return false;
		}
	});

	public void passToLogin() {

		intent = new Intent(getApplicationContext(), LogIn.class);
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				DialogFragment login = new LogIn();
				// login.show(getSupportFragmentManager(), LogIn.TAG);
			}
		}, 500);
	}

	private void loadDataSync(final boolean checkLogin) {
		new AsyncTask<Void, Void, Boolean>() {

			@Override
			protected Boolean doInBackground(Void... params) {

				final String regId = GCMRegistrar
						.getRegistrationId(getApplicationContext());

				// Automatically registers application on startup.
				if (regId.equals("") && EMarketing.getInstance().isLogin()) {
					GCMRegistrar.register(getApplicationContext(),
							Config.GCM_SENDER_ID);
				}

				return EMarketing.getInstance().isLogin();
			}

			@Override
			protected void onPostExecute(Boolean isLogin) {
				if (isLogin) {

					// log that user is log in
					// AnalyticDBHelper.tracked("Logged in");
					//
					// int currentTab = CustomerSupportHelper
					// .getInt(Constant.DEFAULT_TAB);
					int currentTab = 2;
					switch (currentTab) {
					case 0:
						intent = new Intent(getApplicationContext(),
								ClaimActivity.class);
						break;
					case 1:
						intent = new Intent(getApplicationContext(),
								RewardActivity.class);
						break;
					case 2:
						intent = new Intent(getApplicationContext(),
								LearnActivity.class);
						break;
					case 3:
						intent = new Intent(getApplicationContext(),
								LifeStyleActivity.class);
						break;
					case 4:
						intent = new Intent(getApplicationContext(),
								CustomerServiceActivity.class);
						break;
					default:
						intent = new Intent(getApplicationContext(),
								LearnActivity.class);
						break;
					}

					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							startActivity(intent);
							finish();
						}
					}, 500);

					Utility.SyncData();
					NetworkConnectivityReceiver.checkDataSync = true;
				} else {
					if (checkLogin) {
						passToLogin();
					}

				}

			}
		}.execute((Void) null);
	}
}
